package com.yiqi.liebang.feature.home.view;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.suozhang.framework.component.http.RxDataProcessFactory;
import com.suozhang.framework.component.recyclerview.DividerItemDecoration;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.framework.Callback;
import com.suozhang.framework.utils.T;
import com.suozhang.framework.widget.EmptyView;
import com.yiqi.liebang.R;
import com.yiqi.liebang.entity.bo.PeopleBo;
import com.yiqi.liebang.entity.bo.SearchFriendBo;
import com.yiqi.liebang.feature.home.view.adapter.FriendsAdapter;
import com.yiqi.liebang.feature.mine.view.UserInfoActivity;
import com.yiqi.liebang.framework.api.HomeApi;
import com.yiqi.liebang.framework.api.UserApi;

import java.util.List;

import butterknife.BindView;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class AddSuccessActivity extends BaseActivity implements BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.OnItemChildClickListener, Toolbar.OnMenuItemClickListener {


    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.rv_people)
    RecyclerView mRvPeople;
    FriendsAdapter questionAdapter;

    private EmptyView mEmptyView;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_add_success;
    }

    @Override
    protected void initInjector() {

    }


    @Override
    protected void initView() {
        initAdapter();
        mToolbar.inflateMenu(R.menu.menu_complete);
        mToolbar.setOnMenuItemClickListener(this);
        initToolBar(mToolbar, "申请成功！请等待通过", true, true);
    }

    @Override
    protected void initData() {
        getData();
    }

    private void initAdapter() {
        mEmptyView = new EmptyView(mRvPeople);
        mRvPeople.setLayoutManager(new LinearLayoutManager(this));
        mRvPeople.addItemDecoration(new DividerItemDecoration(DividerItemDecoration.VERTICAL).setHeight(2));
        questionAdapter = new FriendsAdapter(true, this);
        questionAdapter.bindToRecyclerView(mRvPeople);
        questionAdapter.setOnItemClickListener(this);
        questionAdapter.setOnItemChildClickListener(this);
        questionAdapter.setCommonCallback(new Callback.CommonCallback() {
            @Override
            public void onSubscribe(Disposable d) {
                showLoading(d);
            }

            @Override
            public void onSuccess(Object result) {
                dismissLoading();
            }

            @Override
            public void onError(Throwable ex) {
                dismissLoading();
            }

            @Override
            public void onComplete() {
                dismissLoading();
            }
        });

    }

    private void getData() {
        AM.api().createApiService(HomeApi.class).tuijianFriends()
                .compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer())
                .subscribe(new Observer<List<SearchFriendBo.UserFriendBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        showLoading(d);
                    }

                    @Override
                    public void onNext(List<SearchFriendBo.UserFriendBean> friendBeans) {
                        dismissLoading();
                        if (friendBeans != null && friendBeans.size() > 0) {
                            questionAdapter.setNewData(friendBeans);
                        } else {
                            questionAdapter.setEmptyView(mEmptyView.getEmptyView("暂无推荐好友"));
                            questionAdapter.setNewData(null);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        dismissLoading();
                        questionAdapter.setEmptyView(mEmptyView.getErrorView(e.getMessage()));
                        questionAdapter.setNewData(null);
                    }

                    @Override
                    public void onComplete() {

                        dismissLoading();
                    }
                });
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Intent intent = new Intent(this, UserInfoActivity.class);
        intent.putExtra("is_view_my", false);
        intent.putExtra("visitorUid", questionAdapter.getData().get(position).getUserUid());
        startActivity(intent);
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        PeopleBo peopleBo = new PeopleBo();
        peopleBo.setApplicationUserUid(questionAdapter.getData().get(position).getUserUid());

        AM.api().createApiService(UserApi.class)
                .getAddFriend(peopleBo)
                .compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        showLoading(d);
                    }

                    @Override
                    public void onNext(String s) {
                        dismissLoading();
                        Intent intent = new Intent(AddSuccessActivity.this, AddSuccessActivity.class);
                        startActivity(intent);
                    }

                    @Override
                    public void onError(Throwable e) {
                        dismissLoading();
                        T.showShort(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        dismissLoading();
                    }
                });
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        finish();
        return false;
    }
}
